<?php
require('functions/page_load.php');
require("fckeditor/fckeditor.php");

$newsletter_nav = true;

if($_SERVER['REQUEST_METHOD'] == 'POST'){

require_once("../classes/class.phpmailer.php");

$email_arr = array();
$name_arr = array();
$seven_days_ago = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d")-7, date("Y")));

if($_POST['users'] == '1'){

	$sql = "SELECT id, email, firstname, surname FROM users WHERE newsletter = 1 AND tradesman != 1 AND (last_emailed < '".$seven_days_ago."' || last_emailed = '0000-00-00')";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	while($rs=mysql_fetch_assoc($query)){
	
	$email_arr[$rs['id']] = $rs['email'];
	$name_arr[$rs['id']] = $rs['firstname'].' '.$rs['surname'];
	
	}
	
}

if($_POST['tradesmen'] == '1'){

	$sql = "SELECT id, email, firstname, surname FROM users WHERE newsletter = 1 AND tradesman = 1 AND (last_emailed < '".$seven_days_ago."' || last_emailed = '0000-00-00')";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	while($rs=mysql_fetch_assoc($query)){
	
	$email_arr[$rs['id']] = $rs['email'];
	$name_arr[$rs['id']] = $rs['firstname'].' '.$rs['surname'];
	
	}
	
}

$html_body = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
</head>
<body style="background-color: #eaeaea;">

	<table width="600" cellpadding="0" cellspacing="0" align="center" style="background-color: #eaeaea;">
	<tr><td valign="bottom" style="font-weight: bold; font-family: arial, sans-serif; font-size: 14px;"><a href="http://www.iwantatradesman.co.uk"><img src="http://www.iwantatradesman.co.uk/images/logo.gif" alt="I Want A Tradesman" border="0"></a>
	<br><br><a href="http://www.iwantatradesman.co.uk" style="color: #002D4E; text-decoration: none;">Home</a> &nbsp;|&nbsp; <a href="http://www.iwantatradesman.co.uk/post-a-job" style="color: #002D4E; text-decoration: none;">Post a job</a> &nbsp;|&nbsp; <a href="http://www.iwantatradesman.co.uk/tradesmen" style="color: #002D4E; text-decoration: none;">Tradesmen</a> &nbsp;|&nbsp; <a href="http://www.iwantatradesman.co.uk/view-jobs" style="color: #002D4E; text-decoration: none;">Jobs</a> &nbsp;|&nbsp; <a href="http://www.iwantatradesman.co.uk/help" style="color: #002D4E; text-decoration: none;">Help</a> &nbsp;|&nbsp; <a href="http://www.iwantatradesman.co.uk/blog" style="color: #002D4E; text-decoration: none;">Blog</a>
	</td></tr>
	<tr><td height="20"></td></tr>
	<tr><td style="color: #666666; font-size: 12px; font-family: arial, sans-serif;" align="left">'.str_replace("\r\n", "<br>", $_POST['content']).'<br><br>
	Best regards<br><br>
	<strong>The I Want A Tradesman team</strong><br><br>
	<a href="http://www.iwantatradesman.co.uk" style="color: #666666;">www.iwantatradesman.co.uk</a></td></tr>
	<tr><td height="20"></td></tr>
	<tr><td style="color: #666666; font-size: 11px; font-family: arial, sans-serif;" align="left">You have received this email because you have requested to receive updates and news from <a href="http://www.iwantatradesman.co.uk" style="color: #666666;">iwantatradesman.co.uk</a>.<br><a href="http://www.iwantatradesman.co.uk/notification-settings" style="color: #666666;">Click here to unsubscribe.</a></td></tr>
	<tr><td height="20"></td></tr>
	<tr><td style="color: #666666; font-size: 11px; font-family: arial, sans-serif;" align="left">This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.</td></tr>
	<tr><td height="20"></td></tr>
	</table>

</body>
</html>';

$plain_text = $_POST['content'].'

Best Regards

The I Want A Tradesman team

http://www.iwantatradesman.co.uk

You have received this email because you have requested to receive updates and news from iwantatradesman.co.uk.	You can unsubscribe at any time by going to: http://www.iwantatradesman.co.uk/notification-settings

This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.';

	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->isHTML(true);
	$mail->Host = "localhost"; // SMTP server
	$mail->From = "mail@iwantatradesman.co.uk";
	$mail->FromName = "I Want A Tradesman";

	$mail->Subject = $_POST['subject'];
	
	$total_emails_sent = 0;
	
	foreach($email_arr AS $key => $val){

	$mail->Body = str_replace("%name%", $name_arr[$key], $html_body);
	$mail->AltBody = str_replace("%name%", $name_arr[$key], $plain_text);
	
	$mail->AddAddress($val);
	$mail->Send();
	$mail->ClearAddresses();
	
	$sql = "UPDATE users SET last_emailed = NOW() WHERE id = '".$key."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	
	$total_emails_sent++;

	}
	
	$results = '<div class="success_result">Email sent successfully to <strong>'.$total_emails_sent.'</strong> recipients.</div>';

}

	$sql = "SELECT last_emailed FROM users ORDER BY last_emailed DESC LIMIT 0,1";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$last_emailed=mysql_fetch_assoc($query);

	$sql = "SELECT id FROM users WHERE newsletter = 1 AND tradesman != 1";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$total_user_newsletters = mysql_num_rows($query);
	
	$sql = "SELECT id FROM users WHERE newsletter = 1 AND tradesman = 1";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$total_tradesman_newsletters = mysql_num_rows($query);

require('includes/header.php');
?>

<div class="side_container">
<h2 class="container_header">Newsletter Stats</h2>

<ul>
<li><strong>Tradesmen: </strong><?php echo number_format($total_tradesman_newsletters); ?></li>
<li><strong>Users: </strong><?php echo number_format($total_user_newsletters); ?></li>
<li><strong>Last email sent: </strong><?php echo date("d/m/Y", strtotime($last_emailed['last_emailed'])); ?></li>
</ul>

</div>

<div class="main_container_wrapper">

	<div class="main_container">
	<h2 class="container_header">Newsletter</h2>
	
	<?php echo $results; ?>
	
	<form method="post" action="">
	
	<div class="form_row_wrapper">
	<div class="form_row_left"><label for="users">Users:</label></div>
	<div class="form_row_right"><input type="checkbox" name="users" id="users" value="1"<?php echo ($_POST['users'] == '1') ? ' checked="checked"' : '' ; ?> /><span style="margin-left: 10px;">Tick this to send the email to all users signed up to the newsletter.</span></div>
	</div>
	
	<div class="form_row_wrapper">
	<div class="form_row_left"><label for="tradesmen">Tradesmen:</label></div>
	<div class="form_row_right"><input type="checkbox" name="tradesmen" id="tradesmen" value="1"<?php echo ($_POST['tradesmen'] == '1') ? ' checked="checked"' : '' ; ?> /><span style="margin-left: 10px;">Tick this to send the email to all tradesmen signed up to the newsletter.</span></div>
	</div>
	
	<div class="form_row_wrapper">
	<div class="form_row_left"><label for="subject">Subject:</label></div>
	<div class="form_row_right"><input type="text" name="subject" id="subject" value="<?php echo $_POST['subject']; ?>" /></div>
	</div>

	<div class="form_row_wrapper">
	<div class="form_row_left"><label for="content">Email:</label></div>
	<div class="form_row_right"><span>Enter %name% to insert the users name. eg; "Dear %name%" will become "Dear John Smith".<br /><br />You do not need to sign off the email as this is already done.</span><br /><textarea name="content" id="content" rows="6" cols="30" class="textbox" style="width: 520px; height: 200px;"><?php echo $_POST['content']; ?></textarea></div>
	</div>
	
	<div class="form_row_wrapper">
	<div class="form_row_left">&nbsp;</div>
	<div class="form_row_right"><input type="image" src="images/send_button.png" alt="Send" /></div>
	</div>
	
	</form>

	</div>

</div>
	
<?php
	require('includes/footer.php');
?>